package org.uni_hi.muus.server.util;

import java.util.Hashtable;

/**
 * Class containing some file extensions and their official
 * MIME-type declaration. 
 * @author Swen
 */
public class MIMEContentType {
	
	public static final String 
		TYPE_TEXT_PLAIN="text/plain",
		TYPE_TEXT_CSS="text/css",
		TYPE_TEXT_HTML="text/html",
		TYPE_TEXT_RICHTEXT="text/richtext",
		TYPE_TEXT_RTF="text/rtf",
		TYPE_TEXT_SGML="text/sgml",
		TYPE_TEXT_XML="text/xml";

		// all image content types
	public static final String
		TYPE_IMAGE_BMP="image/bmp",
		TYPE_IMAGE_GIF="image/gif",
		TYPE_IMAGE_JPEG="image/jpeg",
		TYPE_IMAGE_PNG="image/png",
		TYPE_IMAGE_TIFF="image/tiff",
		TYPE_IMAGE_ICON="image/icon";
		
	// all application content types
	public static final String
		TYPE_APPLICATION_WORD="application/msword",
		TYPE_APPLICATION_OCTET="application/octet-stream",
		TYPE_APPLICATION_ODA="application/oda",
		TYPE_APPLICATION_PDF="application/pdf",
		TYPE_APPLICATION_POSTSCRIPT="application/postscript",
		TYPE_APPLICATION_EXCEL="application/vnd.ms-excel",
		TYPE_APPLICATION_POWERPOINT="application/vnd.ms-powerpoint",
		TYPE_APPLICATION_JAVASCRIPT="application/x-javascript",
		TYPE_APPLICATION_LATEX="application/x-latex",
		TYPE_APPLICATION_SHOCKWAVE="application/x-shockwave-flash",
		TYPE_APPLICATION_TAR="application/x-tar",
		TYPE_APPLICATION_ZIP="application/zip",
		TYPE_APPLICATION_WWW_FORM_DATA="application/x-www-form-urlencoded";
		
	public static final String
		TYPE_AUDIO_BASIC="audio/basic",
		TYPE_AUDIO_MIDI="audio/midi",
		TYPE_AUDIO_MPEG="audio/mpeg",
		TYPE_AUDIO_AIFF="audio/x-aiff",
		TYPE_AUDIO_REALAUDIO="audio/x-pn-realaudio",
		TYPE_AUDIO_REALAUDIO_PLUGIN="audio/x-pn-realaudio-plugin",
		TYPE_AUDIO_WAV="audio/x-wav";
		
	public static final String
		TYPE_VIDEO_MPEG="video/mpeg",
		TYPE_VIDEO_QUICKTIME="video/quicktime",
		TYPE_VIDEO_MSVIDEO="video/x-msvideo",
		TYPE_VIDEO_SGI_MOVIE="video/x-sgi-movie";
	
	private static Hashtable _contentTypes=null;
	
	/**
	 * Initializing the file extensions and their MIME-declaration.
	 * Source: http://www.bolege.de/medientypen/  
	 */
	private static void initHashTable(){
		_contentTypes=new Hashtable();
		
		// all text content types
		_contentTypes.put("",TYPE_TEXT_PLAIN);
		_contentTypes.put("css",TYPE_TEXT_CSS);
		_contentTypes.put("htm",TYPE_TEXT_HTML);
		_contentTypes.put("html",TYPE_TEXT_HTML);
		_contentTypes.put("xhtm",TYPE_TEXT_HTML);
		_contentTypes.put("xhtml",TYPE_TEXT_HTML);
		_contentTypes.put("muus",TYPE_TEXT_HTML);
		_contentTypes.put("txt",TYPE_TEXT_PLAIN);
		_contentTypes.put("rtx",TYPE_TEXT_RICHTEXT);
		_contentTypes.put("rtf",TYPE_TEXT_RTF);
		_contentTypes.put("sgm",TYPE_TEXT_SGML);
		_contentTypes.put("sgml",TYPE_TEXT_SGML);
		_contentTypes.put("xml",TYPE_TEXT_XML);

		// all image content types
		_contentTypes.put("bmp",TYPE_IMAGE_BMP);
		_contentTypes.put("gif",TYPE_IMAGE_GIF);
		_contentTypes.put("jpg",TYPE_IMAGE_JPEG);
		_contentTypes.put("jpeg",TYPE_IMAGE_JPEG);
		_contentTypes.put("jpe",TYPE_IMAGE_JPEG);
		_contentTypes.put("png",TYPE_IMAGE_PNG);
		_contentTypes.put("tif",TYPE_IMAGE_TIFF);
		_contentTypes.put("tiff",TYPE_IMAGE_TIFF);
		_contentTypes.put("ico",TYPE_IMAGE_ICON);
		_contentTypes.put("icon",TYPE_IMAGE_ICON);
		
		// all application content types
		_contentTypes.put("doc",TYPE_APPLICATION_WORD);
		_contentTypes.put("bin",TYPE_APPLICATION_OCTET);
		_contentTypes.put("dms",TYPE_APPLICATION_OCTET);
		_contentTypes.put("lha",TYPE_APPLICATION_OCTET);
		_contentTypes.put("lzh",TYPE_APPLICATION_OCTET);
		_contentTypes.put("exe",TYPE_APPLICATION_OCTET);
		_contentTypes.put("class",TYPE_APPLICATION_OCTET);
		_contentTypes.put("oda",TYPE_APPLICATION_ODA);
		_contentTypes.put("pdf",TYPE_APPLICATION_PDF);
		_contentTypes.put("ai",TYPE_APPLICATION_POSTSCRIPT);
		_contentTypes.put("eps",TYPE_APPLICATION_POSTSCRIPT);
		_contentTypes.put("ps",TYPE_APPLICATION_POSTSCRIPT);
		_contentTypes.put("xls",TYPE_APPLICATION_EXCEL);
		_contentTypes.put("ppt",TYPE_APPLICATION_POWERPOINT);
		_contentTypes.put("js",TYPE_APPLICATION_JAVASCRIPT);
		_contentTypes.put("latex",TYPE_APPLICATION_LATEX);
		_contentTypes.put("swf",TYPE_APPLICATION_SHOCKWAVE);
		_contentTypes.put("tar",TYPE_APPLICATION_TAR);
		_contentTypes.put("zip",TYPE_APPLICATION_ZIP);
		
		_contentTypes.put("au",TYPE_AUDIO_BASIC);
		_contentTypes.put("snd",TYPE_AUDIO_BASIC);
		_contentTypes.put("mid",TYPE_AUDIO_MIDI);
		_contentTypes.put("midi",TYPE_AUDIO_MIDI);
		_contentTypes.put("kar",TYPE_AUDIO_MIDI);
		_contentTypes.put("mpga",TYPE_AUDIO_MPEG);
		_contentTypes.put("mp2",TYPE_AUDIO_MPEG);
		_contentTypes.put("mp3",TYPE_AUDIO_MPEG);
		_contentTypes.put("aif",TYPE_AUDIO_AIFF);
		_contentTypes.put("aiff",TYPE_AUDIO_AIFF);
		_contentTypes.put("aifc",TYPE_AUDIO_AIFF);
		_contentTypes.put("ra",TYPE_AUDIO_REALAUDIO);
		_contentTypes.put("ram",TYPE_AUDIO_REALAUDIO);
		_contentTypes.put("rm",TYPE_AUDIO_REALAUDIO);
		_contentTypes.put("rpm",TYPE_AUDIO_REALAUDIO_PLUGIN);
		_contentTypes.put("wav",TYPE_AUDIO_WAV);
		
		_contentTypes.put("mpe",TYPE_VIDEO_MPEG);
		_contentTypes.put("mpg",TYPE_VIDEO_MPEG);
		_contentTypes.put("mpeg",TYPE_VIDEO_MPEG);
		_contentTypes.put("qt",TYPE_VIDEO_QUICKTIME);
		_contentTypes.put("mov",TYPE_VIDEO_QUICKTIME);
		_contentTypes.put("avi",TYPE_VIDEO_MSVIDEO);
		_contentTypes.put("movie",TYPE_VIDEO_SGI_MOVIE);
	}
	
	private static Hashtable getHashtable(){
		if(_contentTypes==null){
			initHashTable();
		}
		return _contentTypes;
	}
	
	public static String getContentType(String extension){
		String rs=null;
		rs=(String)getHashtable().get(extension);
		if(rs==null)rs=(String)getHashtable().get("");
		return rs;
	}
}
